package com.tulele.vtool.task.process.articlePush;

import com.tulele.vtool.bus.mapper.log.PushDetailLogMapper;
import com.tulele.vtool.bus.mapper.log.PushHeadLogMapper;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.log.PushHeadLog;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.task.async.ThirdTimeArticlePushAsyncTask;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
//1430推送，专业号专用
@Slf4j
@Component
public class Time1430ArticleInfoPushTask extends QuartzJobBean {

    @Autowired
    private ThirdTimeArticlePushAsyncTask thirdTimeArticlePushAsyncTask;

    @Autowired
    private IUserService userService;

    @Value("${sleepTime}")
    private String sleepTimeSet;

    @Autowired
    private PushHeadLogMapper pushHeadLogMapper;

    @Autowired
    private PushDetailLogMapper pushDetailLogMapper;

    @Value("${spring.application.name}")
    private String springProfilesActive;

    @Value("${Professional}")
    private String professional;//专业号

    @SneakyThrows
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        //Random r = new Random();
        //Integer sleepTime= (r.nextInt(5)+5)*1000*60; //随机休眠5-10分钟
        Integer sleepTime = Integer.valueOf(sleepTimeSet) * 1000 * 60;
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        String profiles_str = springProfilesActive;//获取环境
        String[] profile_arr = profiles_str.split(",");
        List<Integer> headIds = new ArrayList<>();
        for (String profile : profile_arr) {

            if(professional.contains(profile)) {//专业号 才推

                List<UserInfo> userList = userService.getUserListByGzhCode(profile);
                Integer sum_count = userList.size();

                PushHeadLog pushHeadLog = new PushHeadLog();
                pushHeadLog.setPushName("下午2点");
                pushHeadLog.setMsgType(4);
                pushHeadLog.setMsgTypeName("下午2点");
                pushHeadLog.setSumCount(sum_count);
                pushHeadLog.setCreateTime(new Date());

                pushHeadLog.setGzhCode(profile);

                pushHeadLogMapper.addPushHeadLog(pushHeadLog);
                Integer headId = pushHeadLog.getId();

                for (UserInfo userInfo : userList) {
                    try {
                        thirdTimeArticlePushAsyncTask.execute("wxPushUser" + userInfo.getUserId(), headId, userInfo, profile);
                    } catch (Exception e) {

                    }
                }
                headIds.add(headId);
            }
        }

        Thread.sleep(1000 * 60 * 30);

        for (Integer headId : headIds) {
            PushHeadLog pushHeadLogU = new PushHeadLog();
            pushHeadLogU.setId(headId);
            pushHeadLogU.setSuccessCount(pushDetailLogMapper.getSuccessCount(headId));
            pushHeadLogU.setErrCount(pushDetailLogMapper.getErrCount(headId));
            pushHeadLogU.setUpdateTime(new Date());
            pushHeadLogU.setEndTime(new Date());

            pushHeadLogMapper.updatePushHeadLog(pushHeadLogU);
        }
    }
}
